<template>
  <div>
    <div class="top_photo">
      <img v-bind:src="'https://weyouth.vip'+photo">
    </div>
    <div class="nav">
      <div class="title">主题：{{title}}</div>
      <input :value="pageTitle" type="hidden" class="pageTitle" id="pageTitle">
      <div class="hr0">
        <span class="morepic"></span>
        {{start_time | formatDate}}至{{end_time | formatDate}}
      </div>
      <div class="hr0">
        <span class="morepic3"></span>
        {{address}}
      </div>
      <div class="hr0">
        <span class="morepic2"></span>
        {{cost}}
      </div>
      <div class="hr0">
        <span class="morepic4"></span>
        {{type}}
      </div>
    </div>
    <div class="mui-content" v-html="content"></div>
    <div class="contact" :data-clipboard-text = "urlText" @click="copyText">
      <div class="us">
        <font>Share</font>
        <div class="contact01"></div>
        <!-- <div class="contact02"></div> -->
        <div class="contact03"></div>
        <!-- <div class="contact04"></div> -->
      </div>
    </div>
    <div class="mui-bar-tab bottomdiv">
      <div class="sponsor">
        <div v-for="(pic,index) in photoArray" :key="index"  @click="list()">
          <img v-bind:src="pic.photo" v-if='index < 3'>
        </div>
        <br>
        <span @click="jump(sid)">
          主办方:{{societyName}}
          <br>
        </span>
      </div>
      <span class="personAll" @click="list()">{{personSum}}人已报名</span>
      <div class="enroll none" id="signUp" @click="up()">报名</div>
      <div class="enroll none" id="signOut" @click="out()">退出</div>
      <div class="enroll" id="edit" @click="edite()">编辑</div>
    </div>
  </div>
</template>
<script>
import { getActivity, getJoinListByAid, isManager, outActivity,
  joinActivity,
  activityState } from '@/api/society'
import { wxShare, wxReady } from '@/utils/wxShare'
import wx from 'weixin-js-sdk'
export default {
  name: 'ActivityDetails',
  data () {
    return {
      aid: '',
      title: '',
      content: '',
      address: '',
      personSum: '',
      societyName: '',
      photo: '',
      start_time: '',
      end_time: '',
      type: '',
      cost: '',
      photoArray: '',
      sid: '',
      pageTitle: '',
      urlText: 'https://weyouth.vip/share/activity/index?id=' + this.$route.query.aid
    }
  },
  created () {
    this.loadData()
    this.sum()
    wxShare()
    this.share()
  },
  filters: {
    formatDate: function (value) {
      return value.slice(0, 16)
    }
  },
  methods: {
    GetQueryString (name) {
      var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
      var r = window.location.search.substr(1).match(reg)
      if (r != null) return unescape(r[2])
      return null
    },
    removeHTMLTag (str) {
      var regex = /<style type="(.*?)">[\s\S]*?<\/style>/g
      str = str.replace(regex, '')
      str = str.replace(/<\/?[^>]*>/g, '') // 去除HTML tag
      str = str.replace(/[ | ]*\n/g, '\n') // 去除行尾空白
      str = str.replace(/ /ig, '') // 去掉
      str.replace(/(^\s*)|(\s*$)/g, '') // 去掉空行
      if (str === '') { str = '<span style="color:grey">[图片]</span>' }
      return str
    },
    randomNum (n) {
      var t = ''
      for (var i = 0; i < n; i++) {
        t += Math.floor(Math.random() * 10)
      }
      return t
    },
    loadData () {
      return getActivity({
        aid: this.$route.query.aid
      }).then(res => {
        const resData = res.data
        this.aid = resData.aid
        this.title = resData.title
        this.pageTitle = resData.title
        this.address = resData.address
        this.content = resData.content
        this.photo = resData.photo
        this.start_time = resData.start_time
        this.end_time = resData.end_time
        this.type = resData.type
        this.cost = resData.cost
        this.activityState()
        let postData = {
          url: window.location.href,
          title: res.data.title
        }
        console.log(postData)
        wx.miniProgram.postMessage({ data: JSON.stringify(postData) })
      }).catch(err => {
        console.log(err)
      })
    },
    jump (sid) {
      this.$router.push('/assn_details?sid=' + sid)
    },
    list () {
      this.$router.push('/join_list?aid=' + this.$route.query.aid)
    },
    edite () {
      this.$router.push('/edit_activity?aid=' + this.$route.query.aid)
    },
    sum () {
      getJoinListByAid(
        {
          aid: this.$route.query.aid
        }
      ).then(res => {
        if (res.code === 1) {
          this.photoArray = res.data.joinList
          this.personSum = res.data.total
          this.societyName = res.data.societyName
          console.log(this.societyName)
          this.sid = res.data.sid
          this.isManager(this.sid)
        }
      }).catch(err => {
        console.log(err)
      })
    },
    isManager (sid) {
      isManager({
        sid: sid
      }).then(res => {
        if (res.code === 1) {
          document.getElementById('edit').style.display = 'block'
        }
      }).catch(err => {
        console.log(err)
      })
    },
    out () {
      outActivity({
        aid: this.$route.query.aid
      }).then(res => {
        if (res.code === 1) {
          alert('退出成功')
          this.$router.go(-1)
        } else {
          alert('退出失败')
        }
      })
    },
    up () {
      joinActivity({
        aid: this.$route.query.aid
      }).then(res => {
        if (res.code === 1) {
          alert('报名成功')
          this.$router.go(-1)
        } else {
          alert('报名失败')
        }
      })
    },
    activityState () {
      activityState({
        aid: this.$route.query.aid
      }).then(res => {
        if (res.code === 0) {
          document.getElementById('signOut').style.display = 'block'
        } else {
          document.getElementById('signUp').style.display = 'block'
        }
      })
    },
    share () {
      wxReady({
        title: this.title,
        desc: this.removeHTMLTag(this.content),
        link: location.href.split('#')[0] + '#' + location.href.split('#')[1] + '&',
        // link: `http://test.qingningmeng.top/dist/#/activity-detail?aid=${this.$route.query.aid}&sid=${this.$route.query.sid}`,
        logo: this.photo
      })
    },
    copyText (e) {
      var clipboard = new this.Clipboard('.contact')
      clipboard.on('success', function (e) {
        console.log(e)
        alert('複製成功！')
      })
      clipboard.on('error', function (e) {
        console.error('Action:', e.action)
        console.error('Trigger:', e.trigger)
        console.log(e)
      })
    }
  }
}
</script>

<style scoped>
@import '../style/css/mui.min';
* {
  margin: 0;
  padding: 0;
}

body {
  background-color: rgb(243, 242, 240) !important;
  font-family: PingFang SC;
}

.mui-content {
  border-radius: 7px;
  padding: 10px;
  width: 95%;
  margin-left: 2.5%;
  font-size: 16px;
  background: white !important;
  margin-bottom: 20px;
}

.title {
  font-size: 20px;
  color: black;
  line-height: 25px;
  padding-left: 7px;
  line-height: 40px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mui-bar-tab {
  height: 35px;
}

.mui-bar {
  position: absolute;
  left: 0px;
  right: 0px;
  bottom: 0px;
  background-color: #fff;
}

.mui-bar {
  position: fixed;
}

.bottomdiv {
  position: fixed;
  z-index: 10;
  height: 63px;
  border-bottom: 0;
  backface-visibility: hidden;
  background: white;
}

.hr0 {
  height: 30px;
  color: grey;
  font-size: 12px;
  text-align: left;
  line-height: 30px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sponsor {
  width: 50%;
  height: 100%;
  display: inline-block;
  float: left;
  text-align: left;
  margin-left: 15px;
  font-size: 20px;
  line-height: 25px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: grey;
  padding-left: 15px;
}

.enroll {
  width: 15%;
  display: inline-block;
  float: right;
  background: rgb(249, 119, 130);
  background: linear-gradient(to right, rgba(240, 84, 73, 60%),rgba(240, 89, 67, 75%),rgba(237, 56, 99, 70%),rgba(238, 57, 98, 80%),rgba(237, 56, 99, 80%));
  color: white;
  text-align: center;
  line-height: 35px;
  border-radius: 5px;
  margin-top: 10%;
  margin-right: 5px;
}

.sponsor span {
  color: black;
  font-size: 16px;
  margin-left: -15px;
}

.site {
  text-align: center;
  color: grey;
  font-size: 13px;
  line-height: 34px;
}

.hr0 div {
  margin-top: -11px;
}

.top_photo {
  margin-top: 20px;
  overflow: hidden;
  width: 95%;
  height: auto;
  margin-left: 2.5%;
  border-radius: 10px;
}

.top_photo img {
  width: 100%;
  height: 100%;
}

.nav {
  margin-top: 20px;
  margin-bottom: 20px;
  width: 95%;
  margin-left: 2.5%;
  height: 170px;
  background: white;
  border-radius: 7px;
  padding: 10px;
}

.sponsor div {
  display: inline-block;
  width: 40px;
  height: 40px;
  border-radius: 20px;
  overflow: hidden;
  margin-top: 15px;
  margin-left: -15px;
}

.sponsor div img {
  width: 40px;
  height: 40px;
}

.personAll {
  width: 100px;
  display: inline-block;
  float: left;
  text-align: left;
  font-size: 17px;
  line-height: 75px;
  color: grey;
  margin-left: -70px;
}

.morepic {
  margin: 3px 5px 0 6px;
  background-image: url(../style/img/22.png);
  background-size: 100% 100%;
  width: 15px;
  height: 20px;
  display: inline-block;
  float: left;
}

.morepic2 {
  margin: 3px 5px 0 3px;
  background-image: url(../style/img/23.png);
  background-size: 100% 100%;
  width: 20px;
  height: 20px;
  display: inline-block;
  float: left;
}

.morepic3 {
  margin: 3px 5px 0 3px;
  background-image: url(../style/img/24.png);
  background-size: 100% 100%;
  width: 20px;
  height: 20px;
  display: inline-block;
  float: left;
}

.morepic4 {
  margin: 3px 5px 0 3px;
  background-image: url(../style/img/25.png);
  background-size: 100% 100%;
  width: 20px;
  height: 20px;
  display: inline-block;
  float: left;
}

#edit {
  float: right;
  display: none;
}

.none {
  display: none;
}
.contact {
  width: 100%;
  height: 144px;
  /*background: url(../style/img/copyright.png) no-repeat;*/
  /*background: #ffffff;*/
  margin-top: 20px;
  margin-bottom: 10px;
  background-size: 100% 100%;
}
.us {
  width: 95%;
  height: 56px;
  /*margin-left: 3%;*/
  margin-left: 2.5%;
  /*background: rgba(255, 255, 255, 0.1);*/
  background-color: white;
  box-shadow: 0px 10px 24px rgba(0, 0, 0, 0.1);
  border-radius: 7px;
}

.us font {
  width: 50%;
  height: 28px;
  font-size: 20px;
  font-weight: 100;
  line-height: 56px;
  color: rgba(22, 22, 22, 1);
  padding-left: 24px;
  float: left;
}

.us div {
  width: 7%;
  height: 44px;
  display: inherit;
  float: left;
  background-size: contain;
  margin-top: 15px;
  margin-left: 10px;
}
.contact01 {
  background: url(../style/img/contact_01.png) no-repeat;
}

.contact02 {
  background: url(../style/img/contact_02.png) no-repeat;
}

.contact03 {
  background: url(../style/img/contact_03.png) no-repeat;
}

.contact04 {
  background: url(../style/img/contact_04.png) no-repeat;
}
</style>
